<?php
/**
 * Created by PhpStorm.
 * User: AA
 * Date: 2016/12/14
 * Time: 16:46
 * * Controller
 *
 */

namespace Home\Controller;

use Think\Controller;
use Think\B_Controller;


class MessageController extends B_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->_message = new \Home\Action\MessageAction();
    }

    /**
     * 添加数据
     * @link /home/message/setMessage
     * @param string $content 消息内容
     * @param int $type 消息类型 1 系统推送 2 用户操作信息
     * @return array
     */

    public function setMessage()
    {
//        $content = trim( "123132定时缴费看上232两件菲利克斯大家");
//        $type = 1;
        $content = trim( $_POST['content'] );
        $type = (int)$_POST['type'];
        $message_id = $this->_message->setMessageText( $content,$type );
        if ( !$message_id )
        {
            return "操作失败";die();
            // return false;die();
        }
        // 判断是否是用户操作
        if ( $type == 2 )
        {
//            $order_id = 1;  // 订单id
//            $action_id = 1; // 活动id
//            $user_id = 1;   // 用户id（C端用户）
//            $store_id = 1;  // 店铺id（B端用户）
        $order_id   = (int)$_POST['order_id'];  // 订单id
        $action_id  = (int)$_POST['action_id']; // 活动id
        $user_id    = (int)$_SESSION['id']?$_SESSION['id']:$_POST['user_id'];   // 用户id（C端用户）
        $store_id   = (int)$_POST['store_id'];  // 店铺id（B端用户）
        $message = $this->_message->setMessage( $order_id,$action_id,$user_id,$store_id,$message_id );
        if ( !$message )
        {
            return "操作失败";die();
            // return false;die();
        }
    }
    echo "操作成功！";die();
    }

    /**
     * 获取当前登录用户的消息 B 端用户
     * @param int $uid 用户id
     * @return array
     */

    public function message()
    {
        $start = (int)$_POST['start'];
        $length = (int)$_POST['length'];
        $id_store = $_SESSION[ $_SESSION['id'].'_storeid']?$_SESSION[ $_SESSION['id'].'_storeid']:'';
        $data = $this->_message->getMessage( $id_store,$start,$length,"B端" );
        $this->assign('data',$data);
        $this->display('message');
    }

    /**
     * 获取当前登录用户的消息 C 端用户
     * @param int $uid 用户id
     * @return array
     */

    public function messagec()
    {
        $uid = (int)$_SESSION['id']?$_SESSION['id']:$_POST['id'];
        $start = (int)$_POST['start'];
        $length = (int)$_POST['length'];
        $data = $this->_message->getMessage( $uid,$start,$length, "C端" );
        $this->assign('data',$data);
        $this->display('message');
    }

    /**
     * 查看某条信息的内容
     * @param int $id 站内信的ID
     * @param int $type 站内信类型 B端 =》2 C端 =》3
     * @return array
     */

    public function content()
    {
        $id = (int)trim($_GET['id']);
        $type = (int)trim($_GET['type']);
        $data = $this->_message->getContent( $id,$type );
        $this->assign('data',$data);
        $this->display('content');
    }


    /**
     * 修改阅读状态
     * @param int $id 站内信id
     * @return array
     */

    public function setRead()
    {
        // $uid = 1;
        $id = (int)trim($_POST['id']);
        $data = $this->_message->setReadStatus( $id,$uid = null  );
        echo $data;
    }

    /**
     * 修改删除状态
     * @param int $id 站内信id
     * @return array
     */

    public function setDel()
    {
        // $uid = 1;
        $id = (int)trim($_POST['id']);
        $data = $this->_message->setDelStatus( $id,$uid = null  );
        echo $data;
    }



}